package com.jumper.study.admin.controller;

import org.springframework.web.bind.annotation.RestController;

import com.jumper.study.admin.dto.resp.HomeResp;
import com.jumper.study.admin.service.AccountService;
import com.jumper.study.admin.service.LessonService;
import com.jumper.study.admin.service.OrderService;
import com.jumper.study.common.api.CommonResult;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.LocalTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


@RestController
@RequestMapping("/api/backend/home")
public class HomeController {
    @Autowired
    private AccountService accountService;

    @Autowired
    private LessonService lessonService;

    @Autowired
    private OrderService orderService;

    @GetMapping("")
    @Cacheable(value = "adminHome#600")
    public CommonResult<HomeResp> index() {
        LocalDateTime start = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0));
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59));
        HomeResp resp = new HomeResp();
        resp.setTodayLogin(accountService.loginCount(start, end));
        resp.setTotalLogin(accountService.loginCount(LocalDate.now()));
        resp.setTodayRegister(accountService.registerCount(start, end));
        resp.setTotalRegister(accountService.registerCount());

        resp.setOnselfLessons(lessonService.lessonCount(true));
        resp.setTotalLessons(lessonService.lessonCount());
        resp.setTodayOrders(orderService.orderCount(start, end));
        resp.setTotalOrders(orderService.orderCount());

        resp.setLoginDatas(accountService.loginDatas(LocalDate.now(), 7));
        resp.setOrderDatas(orderService.orderCountByDay(LocalDate.now(), 7));

        return CommonResult.success(resp);
    }
    
}
